package com.example.glideprogressiveloadimg.util.anim

import android.graphics.Bitmap
import android.view.View
import android.view.animation.AlphaAnimation
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView

interface Displayer {
    fun display(bm: Bitmap?, img: ImageView)
}

class SimpleImageDisplayer : Displayer {
    override fun display(bm: Bitmap?, info: ImageView) {
        if (bm != null && !bm.isRecycled) info.setImageBitmap(bm)
    }
}

class FadeInImageDisplayer @JvmOverloads
constructor(private val durationMillis: Int)
    : Displayer {

    override fun display(bitmap: Bitmap?, imageView: ImageView) {
        imageView.setImageBitmap(bitmap)
        animate(imageView, durationMillis)
    }

    companion object {
        fun animate(imageView: View?, durationMillis: Int) {
            if (imageView != null) {
                val fadeImage = AlphaAnimation(0.0f, 1.0f)
                fadeImage.duration = durationMillis.toLong()
                fadeImage.interpolator = DecelerateInterpolator()
                imageView.startAnimation(fadeImage)
            }
        }
    }
}